package Year22_Month06.day17;

import java.util.LinkedList;
import java.util.Queue;

public class 完全二叉树的节点个数_迭代写法 {
    public int countNodes(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        if(root != null) queue.offer(root);
        int count = 0;//用来记录节点个数
        while(!queue.isEmpty()){
            count++;
            TreeNode node = queue.poll();
            if(node.left != null) queue.offer(node.left);
            if(node.right != null) queue.offer(node.right);
        }
        return count;
    }
}
